package com.shire.shire_interface.service.group.impl;

import com.alibaba.druid.support.json.JSONUtils;
import com.shire.shire_interface.constants.WxConstants;
import com.shire.shire_interface.dao.group.WxGroupInviteDao;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.friend.WxFriend;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupInvite;
import com.shire.shire_interface.model.group.WxGroupInviteExample;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.service.commandcenter.BaseCommandProcessor;
import com.shire.shire_interface.service.commandcenter.WxCommandService;
import com.shire.shire_interface.service.group.WxGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class WxGroupInviteService {
    private static Logger logger = LoggerFactory.getLogger(WxGroupInviteService.class);
    @Resource
    private WxGroupService wxGroupService;
    @Resource
    private WxCommandService commandService;
    @Resource
    private BaseCommandProcessor commandProcessor;
    @Resource
    private WxGroupInviteDao groupInviteDao;
    public void saveGroupInvite(WxGroupInvite wxGroupInvite){
        groupInviteDao.insert(wxGroupInvite);
    }

    public void respInviteGroup(WxCommand wxCommand){
        WxGroupInvite wxGroupInvite = getWxGroupInvite(wxCommand.getId());
        if(wxGroupInvite!=null){
            wxGroupInvite.setCmdstatus(wxCommand.getCmdstatus());
            wxGroupInvite.setRststatus(wxCommand.getRststatus());
            wxGroupInvite.setUpdatetime(new Date());
            groupInviteDao.updateByPrimaryKey(wxGroupInvite);
            // 判断群是否需要群主审核
//            if(wxCommand.getRststatus()== WxErrorCode.GROUP_INVITE_NEED_VERIFY){ // 需要群主验证
//                wxGroupService.updateGroupNeedVerify(wxGroupInvite.getGroupId(), Constants.YesNo.YES.getCode());
//            }
        }
    }

    public WxGroupInvite getWxGroupInvite(Long cmdId){
        WxGroupInviteExample groupInviteExample = new WxGroupInviteExample();
        groupInviteExample.createCriteria().andCmdidEqualTo(cmdId);
        List<WxGroupInvite> inviteList = groupInviteDao.selectByExample(groupInviteExample);
        if (inviteList != null && inviteList.size() > 0){
            return inviteList.get(0);
        }
        return null;
    }

    public WxGroupInvite getGroupInvite(String friendWxId, String groupWxId, Integer csId){
        WxGroupInviteExample groupInviteExample = new WxGroupInviteExample();
        groupInviteExample.createCriteria().andCsidEqualTo(csId).andFriendwxidEqualTo(friendWxId).andGroupnoEqualTo(groupWxId);
        List<WxGroupInvite> inviteList = groupInviteDao.selectByExample(groupInviteExample);
        if (inviteList != null && inviteList.size() > 0){
            return inviteList.get(0);
        }
        return null;
    }

//    public int getInvitedGroupNumByAccountToday(String wxAccount){
//        String hql = "from WxGroupInvite where wxAccount=:wxAccount and rstStatus<=0 and createTime>=:createTime";
//        Long count = controller.getResultCount(hql, new KeyValue<>("wxAccount", wxAccount), new KeyValue<>("createTime", DateUtils.getToday()));
//        return count.intValue();
//    }

    /**
     * 生成邀请好友进群指令
     * @return
     */
    public WxGroupInvite genInviteGroupCmd(Integer csId, WxAccount wxAccount, WxFriend wxFriend, WxGroup wxGroup){
        // 验证是否已经邀请过该群
        WxGroupInvite invite = getGroupInvite(wxFriend.getFriendwxid(), wxGroup.getGroupno(),csId);
        if(invite!=null){
            return null;
        }
        String applyMsg = "";
        WxGroupInvite newInvite = new WxGroupInvite();
        newInvite.setWxaccount(wxAccount.getWxid());
        newInvite.setFriendid(wxFriend.getId());
        newInvite.setFriendwxid(wxFriend.getFriendwxid());
        newInvite.setFriendremark(wxFriend.getFriendremark());
        newInvite.setGroupname(wxGroup.getGroupname());
        newInvite.setGroupno(wxGroup.getGroupno());
        newInvite.setApplymsg(applyMsg); // 获取验证消息话术
        newInvite.setCreatetime(new Date());
        newInvite.setUpdatetime(newInvite.getCreatetime());
        newInvite.setCsid(csId);

        // 添加指令库
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("groupWxId", wxGroup.getGroupno());
        reqMap.put("userWxIds", wxFriend.getFriendwxid());
        reqMap.put("inviteMessage","你好");
        WxCommand wxCommand = commandService.generateCmdObject(wxAccount.getCsid(), wxAccount.getId(), JSONUtils.toJSONString(reqMap),WxConstants.CmdTaskType.UNKNOWN, 0, WxConstants.CmdType.CRK_INVITE_GROUP_D, WxConstants.CmdPriority.UNKNOWN);
        if(wxCommand==null){
            logger.info("邀请好友进群失败，wxAccount={}, friendRemark={}, groupName={}", wxAccount.getAccount(), wxFriend.getFriendremark(), wxGroup.getGroupname());
            return null;
        }
        commandProcessor.addCommand(wxAccount.getCsid(),wxAccount.getId(),wxCommand);
        newInvite.setCmdid(wxCommand.getId());
        newInvite.setCmdstatus(wxCommand.getCmdstatus());
        newInvite.setRststatus(wxCommand.getRststatus());
        saveGroupInvite(newInvite);
        return newInvite;
    }

    public List<WxGroupInvite> getInvitedGroupsByFriendId(String friendWxid, Integer csId){
        WxGroupInviteExample inviteExample = new WxGroupInviteExample();
        inviteExample.createCriteria().andFriendwxidEqualTo(friendWxid);
        return groupInviteDao.selectByExample(inviteExample);
    }
}
